package com.cdl.utils;

import com.alibaba.druid.pool.DruidDataSource;
import org.eobjects.metamodel.DataContext;
import org.eobjects.metamodel.DataContextFactory;
import org.eobjects.metamodel.jdbc.JdbcDataContext;
import org.eobjects.metamodel.schema.Column;
import org.eobjects.metamodel.schema.Schema;
import org.eobjects.metamodel.schema.Table;
import org.eobjects.metamodel.schema.TableType;

import javax.sql.DataSource;
import java.util.HashMap;

/**
 * @Author: chendongliang
 * @Date: 2019/8/30 16:20
 */
public class MetaModelTest {


    public static void main(String[] args) {
        getMetaInfos();

        HashMap<String,String>  hashTable = new HashMap<String,String>();

    }


    public static DataSource getDataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        druidDataSource.setUrl("jdbc:mysql://119.3.62.50:3306/eslink_safecheck_old?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&autoReconnect=true");
        druidDataSource.setUsername("eslink123");
        druidDataSource.setPassword("Eslink@123");
        return druidDataSource;
    }


    public static void getMetaInfos() {
        DataSource dataSource = getDataSource() ;
        //DataContext dataContext = new JdbcDataContext(dataSource, new TableType[]{},"yunbi_sys");
        DataContext dataContext = DataContextFactory.createJdbcDataContext(dataSource);
        System.out.println("default:"+dataContext.getDefaultSchema());

        Schema[] schemas = dataContext.getSchemas();
        for (Schema schema : schemas) {
            System.out.println("-----"+schema.getName());
            if(!"eslink_safecheck_old".equalsIgnoreCase(schema.getName())){
                continue;
            }
            Table[] tables = schema.getTables();
            for (Table table : tables) {
                System.out.println("    "+table.getName());
                Column[] columns = table.getColumns();
                for (Column column : columns) {
                    System.out.println("        "+column+" --  "+column.getRemarks());
                }
            }
        }

    }






}
